package com.geek.novel.utils;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;
import android.provider.Settings;
import android.view.Window;
import android.view.WindowManager;
import androidx.core.math.MathUtils;

/**
 * 调节屏幕亮度工具类
 */
public class BrightUtil {

    /**
     * 获取当前系统亮度的百分比
     */
    public static  int getSystemBrightnessPercent(Activity context){
        int result = getSystemBrightness(context);
        result = Math.max(result, 0);
        int brightnessMax = getBrightnessMax();
        return (int)(((float) result)/brightnessMax * 100);
    }

    /**
     * 获取当前系统亮度
     * 返回-1视为获取失败
     * @return
     */
    public static int getSystemBrightness(Activity context){
        if(null != context) {
            try {
                return Settings.System.getInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
            } catch (Settings.SettingNotFoundException e) {
                e.printStackTrace();
            }
        }
        return -1;
    }

    /**
     * 获取最大亮度
     * @return max
     */
    private static int getBrightnessMax() {
        try {
            Resources system = Resources.getSystem();
            int resId = system.getIdentifier("config_screenBrightnessSettingMaximum", "integer", "android");
            if (resId != 0) {
                return system.getInteger(resId);
            }
        }catch (Exception ignore){}
        return 255;
    }


    /**
     * 清除系统的亮度设置
     */
    public static void resetSystemBrightnessPercent(Activity context) {
        float none = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
        Window window = context.getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.screenBrightness = none;
        window.setAttributes(layoutParams);
    }

    /**
     * 改变当前系统亮度(百分比)
     */
    public static void setSystemBrightnessPercent(Activity context, int progress) {
        int brightnessMax = getBrightnessMax();
        int brightness = (int)(((float)progress)/100 * brightnessMax);
        setSystemBrightness(context, brightness);
    }

    /**
     * 改变当前系统亮度-最大亮度为255 -注意：当为自动亮度调节模式下，手动设置无效，需将自动亮度调节先关闭
     * @return
     */
    public static void setSystemBrightness(Activity context, int brightness) {
        if(null != context) {
            int brightnessMax = getBrightnessMax();
            if(brightness>=brightnessMax){
                brightness=brightnessMax;
            }
            if(brightness<=0){
                brightness=0;
            }
            Window window = context.getWindow();
            WindowManager.LayoutParams layoutParams = window.getAttributes();
            layoutParams.screenBrightness = (float)brightness / brightnessMax;
            window.setAttributes(layoutParams);
        }
    }

    /**
     * 监听亮度的改变
     */
    public static class BrightnessObserver extends ContentObserver {
        private Activity mContext;
        private OnBrightnessChangeListener mOnBrightnessChangeListener;
        //创建实例所需参数
        public BrightnessObserver(Activity context, Handler handler, OnBrightnessChangeListener onBrightnessChangeListener) {
            super(handler);
            this.mContext = context;
            this.mOnBrightnessChangeListener = onBrightnessChangeListener;
            register();
        }
        //注册监听
        public void register(){
            if(mContext != null) {
                Uri brightnessUri = Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS);
                mContext.getContentResolver().registerContentObserver(brightnessUri, true, this);
            }
        }
        //取消注册监听
        public void unregister(){
            if(mContext != null) {
                mContext.getContentResolver().unregisterContentObserver(this);
            }
        }
        //通过接口返回当前亮度
        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);
            if(null != mOnBrightnessChangeListener && null != mContext){  //通过接口返回当前亮度
                mOnBrightnessChangeListener.onChange(getSystemBrightnessPercent(mContext));
            }
        }
    }

    /**
     * 监听亮度改变-接口
     */
    public interface OnBrightnessChangeListener {
        void onChange(int brightness);
    }

}